/**
 * zmn角色模块-角色权限树状列表
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author heguanghua
 * @since 2020/05/12 17:10
 */
let editObj = null, ptable = null, treeGrid = null, tableId = 'zmn-table', layer = null;
layui.define(['form', 'table', 'zmnApp', 'treeGrid'], function (exports) {
    let $ = layui.jquery;
    let form = layui.form;
    let layer = layui.layer;
    let treeGrid = layui.treeGrid;
    let zmnApp = layui.zmnApp;

    ptable = treeGrid.render({
        id: tableId,
        elem: '#' + tableId,
        url: '/role/permit/ajaxTree.action?roleId=' + json.roleId,
        cellMinWidth: 100,
        idField: 'moduleId',                //必須字段
        treeId: 'moduleId',                 //树形id字段名称
        treeUpId: 'parentId',               //树形父id字段名称
        treeShowName: 'moduleName',         //以树形式显示的字段
        heightRemove: [".dHead", 235],       //不计算的高度,表格设定的是固定高度，此项不生效
        //height: '100%',
        isFilter: false,
        iconOpen: false,                    //是否显示图标【默认显示】
        isOpenDefault: true,                //节点默认是展开还是折叠【默认展开】
        loading: true,
        method: 'POST',
        isPage: false,
        cols: [[
            {field: '', title: '快选', width: 50, align: 'center', sort: false, templet: function(d) {
                if(d.level > 0) {
                    return '<input type="checkbox" name="layTableCheckbox333" value="' + d.LAY_INDEX + '" ' + (d.checked ? 'checked' : '') + ' lay-skin="primary"/>';
                }
                return '';
            }},
            {field: 'moduleId', title: '模块ID', width: 80, align: 'center', sort: false},
            {field: 'moduleName', title: '模块名称', width: 180, sort: false},
            {fixed: 'right', title: '权限列表', minWidth: 180, align: 'left', templet: function(d) {
                let html = '';
                d.permits.forEach(function (item, i) {
                    html += '<input type="checkbox" name="permit" data-type="' + d.moduleId + '" value="' + item.permitId + '" ' + (item.checked ? 'checked' : '') + ' title="' + item.permitName + '"/>'
                });
                return html;
            }}
        ]],
        parseData: function (res) {     // 数据加载后回调
            res.data.forEach(function(item) {
                if(item.level > 0) {
                    item.lay_is_open = false;
                }
            });
            return { "message": res.message, "code": res.status,  "data": res.data, "count": 0, "is": true, "tip": "OK"};
        }
    });

    // 快速选择
    treeGrid.on('checkbox(zmn-table)', function(obj) {
        let moduleId = obj.data.moduleId;
        let checked = obj.checked;
        let $permits = $('input[data-type='+ moduleId +']');
        if(!$permits || $permits.length == 0) {
            return;
        }
        $permits.each(function (i) {
            let $this = $(this);
            $this.prop({checked:checked});
            //$(this).attr("checked", checked);
        });
        layui.form.render('checkbox');
    });

    // 快速选择
    checkPermit = function(el, moduleId) {
        alert(moduleId);
        return;
        let $permits = $('input[data-type='+ moduleId +']');
        if(!$permits || $permits.length == 0) {
            return;
        }
        let checked = $(el).prop('checked');
        $permits.each(function (i) {
            $(this).prop('checked', checked);
        });
    };

    // 分配权限
    assignPermit = function() {
        let $ = layui.$;
        var $permitIds = $('input[name="permit"]');
        var permitIds = new Array();
        if($permitIds.length > 0) {
            $permitIds.each(function(i) {
                let $this = $(this);
                if($this.prop('checked')) { permitIds.push($(this).val()); }
            });
        }
        if(permitIds == null || permitIds.length == 0) {
            layui.layer.msg("请选择要分配的权限");
            return;
        }
        zmnPage.ajaxSimple(zmnApp.SYS_BASE_URL +"/role/permit/modify/assign.action", { roleId: json.roleId, permitIds: permitIds.join(",")}, 1500);
    };

    //对外暴露的接口
    exports('role_permit_tree', {});
});